import {Command} from '../constant';
import Taro, {getCurrentInstance} from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async page() {
      let {user} = Taro.getStorageSync('b2b-wechat@login');
      // let res = await api.shopController.getPayType();
      // if (res.code == 'K-000000') {
      //   // action.commonChange('main.infoMessage', res.context.noticeNum)
      // }
      if (user?.wxOpenId) {
        await action.goToWeixinPay(user?.wxOpenId);
      }
    },

    //小程序支付
    async goToWeixinPay(openid) {
      let {tid, payState, parentTid} = getCurrentInstance().router.params;
      const params = {tid, openid, parentTid};
      const param = {
        parentTid: parentTid,
        isExamine: false,
        // 支付状态-判断是否尾款支付
        payState: payState,
        tid: tid,
      };
      try {
        const result = await api.payController.wxPayUnifiedorderForLittleProgram(params);
        if (result.code === 'K-000000') {
          const {timeStamp, nonceStr, signType, paySign} = result.context;
          await Taro.requestPayment({
            //@ts-ignore
            timeStamp,
            nonceStr,
            package: result.context.package,
            signType,
            paySign,
            success: async (res) => {
              let {shopToken} = Taro.getStorageSync('b2b-wechat@login');
              let WANMI_LINK = 'https://ruizhi-test.jsruiyin.com/mobile/pages/';
              let appUrl =
                WANMI_LINK +
                `package-C/order/order-tool/order-success/index?zhsq=true&shopToken=${shopToken}&param=${JSON.stringify(
                  param,
                )}`;
              //微信支付成功的回调
              // const context = await api.payController.getWeiXinPayOrderState(tradeNo ? tradeNo : parentTid);
              // if (context == 1) {
              //   await Taro.navigateTo({
              //     url: '/pages/package-C/order/order-list/index',
              //   });
              // }
              Taro.setStorageSync('url', appUrl);
              Taro.redirectTo({
                url: '/pages/package-A/webview/index',
              });
            },
            fail: function (res) {},
          });
        } else {
          let {shopToken} = Taro.getStorageSync('b2b-wechat@login');
          let WANMI_LINK = 'https://ruizhi-test.jsruiyin.com/mobile/pages/';
          let appUrl = WANMI_LINK + 'package-C/order/order-list/index?zhsq=true&shopToken=' + shopToken;
          Taro.showModal({
            title: '温馨提示',
            content: result.message,
            success: function (res) {
              if (res.confirm) {
                Taro.setStorageSync('url', appUrl);
                Taro.redirectTo({
                  url: '/pages/package-A/webview/index',
                });
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            },
            confirmColor: '#218CFF',
          });
        }
      } catch (e) {
        // Taro.showToast({
        //   title: '支付失败！',
        //   icon: 'none',
        //   duration: 2000,
        // });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAPay'),
  };
}

//create by moon https://github.com/creasy2010/moon
